// var mongoose = require('mongoose');
// var Uplpad = require('../models/upload.model')// 引数据模型
// var multer=require('multer')
// var path=require('path');

// var storage = multer.diskStorage({
//         destination: function (req, file, cb) {
//         cb(null, './uploads')
//         },
//         filename: function (req, file, cb) {
//         cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
//         }
//         })

// //上传
// exports.upload = function(req,res,next){
   
//     var upload = multer({ storage: storage,
//         fileFilter:function(req,file,callback){
//             var ext=path.extname(file.originalname);
//             if(ext!=='.png'&& ext!=='.jpg'&&  ext!=='.gif'){
//                 return callback(res.end('文件类型不符合'));
//             }
//             callback(null,true);
//         }
//      }).single('avatar')
//     upload(req,res,function(err){
//         console.log(req.file);
//          var upload=new Uplpad(req.file);
//          upload.save(err=>{
//             if(!err){
//                 console.log('success');
//             }else{
//                 console.log(err);
//             }
//          }).then(data=>{
//             console.log(data);
//          });
//         res.end('file uploaded');
//     })
    
// }
// //修改
// exports.update = function(req,res,next){
  
//     var id = req.params.id; // req.param('id')
//     // 现在操作的都是mongoose而不是mongodb
//     Uplpad.findByIdAndUpdate(id,{$set: req.body}, {new:false} )
//     .then(data=>{
//         res.json({msg:'数据修改成功',status:200}); // 如果你要返回数据，这个data返回的是你之前修改的那个数据内容
//     })
// }


// //删除
// exports.remove = function(req,res,next){
//     var id = req.params.id;
//     Uplpad.findByIdAndRemove(id,function(err,doc){
//         res.json({msg:'数据删除成功',status:200})
//     })
// } 


// exports.list = function(req,res,next){
//     var page = (req.body.page) ? req.body.page : 1;
//     var rows = (req.body.rows) ? req.body.rows : 2;

//     var queryCondition = {};
//     var originalname;
//     if( req.body.originalname && req.body.originalname.trim().length>0 ){
//         name = req.body.name;
//         queryCondition = {"originalname":originalname}
//     }
//     Uplpad.paginate(queryCondition, { page: page, limit: parseInt(rows) }, function(err, result) {
//         result.rows = result.docs;
//         delete result.docs;
//         res.json(result);
//     });
// }



var mongoose = require('mongoose');
var multer  = require('multer');
var path  = require('path');
const DataModel  = require('../models/upload.model');




var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now()+ path.extname(file.originalname))
  }
})

exports.upload = function(req,res,next){
    
    var upload = multer({
        storage:storage
    }).single('avater');

    upload(req,res,function(err){
        const dataModel = new DataModel(req.file);

        dataModel.save()
        .then(data=>{
             res.json(data);
        })

        res.end('File is uploaded')
    })
}


exports.list = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var rows = (req.body.rows) ? req.body.rows : 2;

    var queryCondition = {};
    var originalname;
    if( req.body.originalname && req.body.originalname.trim().length>0 ){

        name = req.body.name;
        queryCondition = {"originalname":originalname}
    }
    
    DataModel.paginate(queryCondition, { page: page, limit: parseInt(rows) }, function(err, result) {
    
        console.log(123)
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    });
}